package acl.hibernate.dao;



import java.util.List;

import acl.hibernate.po.OaDepartments;
import acl.hibernate.po.OaPermissions;
import acl.hibernate.po.OaUsers;
import acl.hibernate.po.UserView;
import hibernate.HibernateBaseDAO;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class ACLdao extends HibernateBaseDAO{
	
	Session session = null;
	

	

	public OaUsers getUserByUserName(String userName) {// 
		// Author:Silence
		//SessionFactory factory = config.buildSessionFactory();
		logger.debug("getUserByUserName:"+userName);
		OaUsers user = null;
		try {
			session = this.getSession();		
			String hql = "From OaUsers as user WHERE user.username=:userName";
			Query q = session.createQuery(hql);
			q.setString("userName", userName);
			if(q!=null)
				if( q.list().size()>0)
					user = (OaUsers) q.list().get(0);
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{			
			this.closeSession(session);
		}
		return user;
	}

	// -------------------------------------------------------------
	public OaUsers getUserById(Integer id) {// 
		logger.debug("getUserByUserId:"+id);
		
		session = this.getSession();
		OaUsers user = null;
		String hql = "From OaUsers as user WHERE user.id="+id;
		Query q = session.createQuery(hql);
		try {
			user = (OaUsers) q.list().get(0);
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{			
			this.closeSession(session);
		}
		return user;
	}
	// -------------------------------------------------------------
	public List getAllPermission() {// 
		logger.debug("getAllPermission");
		
		session = this.getSession();
		List result = null;
		Query query = null;
		String hql = "from OaPermissions as op order by op.code";
		query = session.createQuery(hql);
		try {
			result = query.list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{			
			this.closeSession(session);
		}
		return result;

	}

	// -------------------------------------------------------------
	public List getUserRoles(Integer id) {// 
		logger.debug("getUserRoles");
		session = this.getSession();
		List result = null;
		Query query = null;
		String hql = "from OaUsersRoles as oaUsersRoles where oaUsersRoles.userId="+ id;
		query = session.createQuery(hql);
		try {
			result = query.list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			
			this.closeSession(session);
		}
		return result;

	}

	// -------------------------------------------------------------
	public List getRolePermissions(Integer id) {// 
		logger.debug("getRolePermissions");
		session = this.getSession();
		List result = null;
		Query query = null;
		String hql = "from OaRolesPermissions as oaRolesPermissions where oaRolesPermissions.roleId="
				+ id;
		query = session.createQuery(hql);
		try {
			result = query.list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			this.closeSession(session);
		}
		return result;

	}

	// -------------------------------------------------------------
	public void deleteUserById(Integer id) {
		logger.debug("deleteUserById："+id);
		session = this.getSession();
		String hql = "From OaUsers WHERE id = "+id;
		Query q = session.createQuery(hql);
		List l = q.list();
		OaUsers o = (OaUsers) l.get(0);
		String a = "0";
		o.setUserstate(0);
		Transaction tr = null;
		try{
			tr=session.beginTransaction();
			session.update(o);
			tr.commit();
		}catch(Exception e){
			if(tr!=null)
				tr.rollback();
			e.printStackTrace();
			this.closeSession(session);
			return;
		}		
	}

	// -------------------------------------------------------------
	/*
	 * 获取某部门所有人员，但不包括当前操作人员
	 */
	public List getUserListByXysh_id(Integer yxsh_id,Integer userId) {
		// 根据单位读取用户列表
		logger.debug("getUserListByXysh_id："+yxsh_id+","+userId);
		session = this.getSession();
		List result = null;
		try {
			String hql = "From OaUsers as user WHERE user.yxshId=:yxsh_id and user.id!="+userId+" and user.userstate=1";
			Query q = session.createQuery(hql);
			q.setInteger("yxsh_id", yxsh_id);
			result = q.list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			this.closeSession(session);
		}
		return result;

	}
	public OaDepartments getDepartmentbyid(Integer id) {
		// 根据id读取部门信息
		// Author:Silence
		//Time:09-3-12
		logger.debug("getDepartmentbyid:"+id);
		session = this.getSession();
		OaDepartments department = null;
		String hql = "From OaDepartments as department WHERE department.id="+id;
		Query q = session.createQuery(hql);
		try {
			department = (OaDepartments) q.list().get(0);
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			this.closeSession(session);
		}
		return department;
	}
	// -------------------------------------------------------------
	public List getSmallPermission(List l){
		session = this.getSession();
		List r = null;
		String hql = "From OaPermissions  as d WHERE d.replace="+((OaPermissions)l.get(0)).getId();
		for(int i = 1;i<l.size();i++){
			hql += " or d.replace="+((OaPermissions)l.get(i)).getId();
		}
		try{
			Query q = session.createQuery(hql);
			r = q.list();
		}catch(Exception e){
			e.printStackTrace();		
		}finally{
			this.closeSession(session);
		}
		return r;
	}
	//-----------------------------------------------------------------
	public void deleteCheckedUserByIds(String ids) {
		// 批量删除用户
		// 思路：将用户状态设定为0
		// Author:Never
		session = this.getSession();
		String[] id=ids.split(",");
		Transaction tr = null;
		try{
			tr=session.beginTransaction();
			for(int i=0;i<id.length;i++){
				if(!id[i].equals("-1")){					
					String hql = "From OaUsers WHERE id = "+id[i];
					Query q = session.createQuery(hql);
					List l = q.list();
					OaUsers o = (OaUsers) l.get(0);
					o.setUserstate(0);
					session.update(o);					
				}
			}
			tr.commit();
		}catch(Exception e){
			if(tr!=null)
				tr.rollback();
			e.printStackTrace();
			this.closeSession(session);
			return;
		}
	}
	public void deleteCheckedUserByIds(String[] ids) {
		// 批量删除用户
		// 思路：将用户状态设定为0
		// Author:Never
		session = this.getSession();
		Transaction tr =null;
		try{
			tr=session.beginTransaction();
			
			for(int i=0;i<ids.length;i++){
				if(!ids[i].equals("-1")){									
					String hql = "From OaUsers WHERE id = "+ids[i];
					Query q = session.createQuery(hql);
					List l = q.list();
					OaUsers o = (OaUsers) l.get(0);
					o.setUserstate(0);
					session.update(o);						
				}
			}	
			tr.commit();
		}catch(Exception e){
			if(tr!=null)
				tr.rollback();
			e.printStackTrace();
			this.closeSession(session);
			return;
		}
	}

}
